package com.mydemo.charging.order.amqp;

import com.mydemo.charging.common.constant.AmqpComponentConst;
import com.mydemo.charging.common.pojo.message.DelayCheckMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class AmqpConsumer {
    @Autowired
    private OrderConsumerService orderConsumerService;
    @Autowired
    private RedisTemplate redisTemplate;
    //消费订单延迟消息的消费方法
    //监听死信队列
    @RabbitListener(queues= AmqpComponentConst.DELAY_BILL_DL_QUEUE)
    public void consumeDlMessage(DelayCheckMessage msg, Message message, Channel channel){
        //考虑消费业务逻辑 实现消费
        //TODO 抢锁
        log.debug("死信队列消息:{}",msg);
        orderConsumerService.handleDelayResult(msg);
        //TODO 释放锁
        //除了可以获取消息体反序列化对象 还可以使用message
        //执行成功 手动确认
        try {
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
            log.error("死信队列消息确认失败失败");
            throw new RuntimeException(e);
        }
        //执行失败 手动反馈拒绝 消息会重新投递 TODO
    }
}
